package com.yicheng.controller;

import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class StudentHandler {

//    @Resource
//    private RedisTemplate<String, Student> redisTemplate;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

//    @PostMapping("/set")
//    public int set(@RequestBody Student student) {
//        redisTemplate.opsForValue().set("student", student, 10, TimeUnit.SECONDS);
//        return 3;
//    }
//
//    @GetMapping("/get/{key}")
//    public Student get(@PathVariable(value = "key") String key) {
//        return redisTemplate.opsForValue().get(key);
//    }
//
//    @DeleteMapping("/delete/{key}")
//    public boolean delete(@PathVariable(value = "key") String key) {
//        redisTemplate.delete(key);
//        return !redisTemplate.hasKey(key);
//    }

    @GetMapping("/list")
    public List<String> listTest(){
        ListOperations<String, String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list","三");
        listOperations.leftPush("list","四");
        listOperations.leftPush("list","wu");
        return listOperations.range("list",1,2);
    }
//
//    @GetMapping("/set")
//    public Set<String> setTest(){
//        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
//        setOperations.add("set","1");
//        setOperations.add("set","1");
//        setOperations.add("set","2");
//        return setOperations.members("set");
//    }
//
//    //有序集合
//    @GetMapping("/zset")
//    public Set<String> zsetTest(){
//        //有序集合需要手动设置值的顺序。
//        ZSetOperations<String, String> zsetOperations = redisTemplate.opsForZSet();
//        zsetOperations.add("zset","1",1);
//        zsetOperations.add("zset","2",2);
//        zsetOperations.add("zset","3",3);
//        return zsetOperations.range("zset",0,2);
//    }
//
//    @GetMapping("/hash")
//    public Map<String, String> hashMapTest(){
//        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
//        hashOperations.put("key1","a","hello");
//        hashOperations.put("key1","b","hello");
//        hashOperations.put("key1","c","hello");
////        return hashOperations.get("key1","a");//get取key1下的a下的值
//        return hashOperations.entries("key1");//entries取整个key1下的map
//
//    }
}
